package definition

import (
	"codejy/globalfunc"
	"codejy/structdef"
	"codejy/utils"
)

func init() {
	globalfunc.RegisterFuncAll("DefanalysisMode", DefanalysisMode)
}

type Defanalysis interface {
	//执行定义解析
	ParseDefinition(codes *[]structdef.LineCodeDefinition, sealityFilePath string)
	//优先级获取 ,,0最大优先级,越大优先级越低
	GetPriority() int
}

var definitions = []Defanalysis{}

// 注册定义
func RegisterDefinition(d Defanalysis) {
	//判断定义是否已经注册
	for _, v := range definitions {
		if v == d {
			return
		}
	}
	//注册定义
	definitions = append(definitions, d)
	//按照优先级从小到大排序
	utils.SortSlice[Defanalysis](&definitions, 0, func(i, j Defanalysis) int {
		return i.GetPriority() - j.GetPriority()
	})
}

// 策略模式
func DefanalysisMode(codes *[]structdef.LineCodeDefinition, sealityFilePath string) {
	//循环策略列表
	for _, v := range definitions {
		//执行定义解析
		v.ParseDefinition(codes, sealityFilePath)
	}
}
